package wechatpay

import (
	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
	"log"
)

type Refund struct {
	OutTradeNo  string `json:"outTradeNo"`
	OutRefundNo string `json:"outRefundNo"`
	Total       int64  `json:"total"`
	Refund      int64  `json:"refund"`
}

func (refund *Refund) Done() (string, error) {
	svc := refunddomestic.RefundsApiService{Client: MyClient.WechatClient}
	resp, result, err := svc.Create(MyClient.Ctx,
		refunddomestic.CreateRequest{
			OutTradeNo:  core.String(refund.OutTradeNo),
			OutRefundNo: core.String(refund.OutRefundNo),
			//NotifyUrl:   core.String("https://weixin.qq.com"),
			Amount: &refunddomestic.AmountReq{
				Currency: core.String("CNY"),
				From: []refunddomestic.FundsFromItem{refunddomestic.FundsFromItem{
					Account: refunddomestic.ACCOUNT_AVAILABLE.Ptr(),
					Amount:  core.Int64(444),
				}},
				Refund: core.Int64(refund.Refund),
				Total:  core.Int64(refund.Total),
			},
		},
	)
	if err != nil {
		// 处理错误
		log.Printf("call Create err:%s", err)
		return "", err
	} else {
		// 处理返回结果
		log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
		return string(*resp.Status), nil
	}
}
